<%@ page language="java" contentType="text/html; charset=utf-8"%>
<html>
	<head>
		<%@ include file="/extjs/include/all.jsp"%>
		<link href="${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/userModule/page/css/authority.css" rel="stylesheet" type="text/css">
		<title>角色管理</title>
		<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/js/validate.js">
		</script>
	</head>
	<body>
		<div id="roleForm" style="width:100%"></div>
		<div id="result" style="width:100%"></div>
		<div id="selectRole" style="width:100%"></div>
		<div id="selectResource" style="width:100%"></div>
	</body>
</html>

<script type="text/javascript">
var submitFunc;
var submitFuncResource;
	
Ext.onReady(function(){

  	Ext.QuickTips.init();   	

	//-------------------------------------------------------------------------
	// 			查询角色
	//
	//-------------------------------------------------------------------------
		
    var top = new Ext.FormPanel({

        frame:true,		//frame为false 背景为白色 
        title: '查询角色',
        bodyStyle:'padding:5px 5px 0',
        collapsible:true,
        renderTo:'roleForm',  //选择div
        items: [{
            layout:'column',
            items:[{
                columnWidth:.3,
                layout: 'form',
                items: [{
                    xtype:'textfield',
                    fieldLabel: '角色ID',
                    name: 'roleId_filter',
                    id:'roleId_filter',
                    maxLength:10,
                    vtype: 'isNumber',
                    anchor:'95%'
                }]
            }]
        }],

        buttons: [{
            text: '查询',
            handler:function(){
            	if (top.form.isValid()) {
		       		ds.load({params:{						
		       				start:0, limit:20,
								roleId:Ext.get("roleId_filter").dom.value
						}}); 
		
		       		grid.getView().refresh();
       			} else {
       				Ext.MessageBox.alert('警告','对不起,输入查询条件有误！');
       			}
            }
        }]
    });
    
    //-------------------------------------------------------------------------
	// 			角色列表
	//
	//-------------------------------------------------------------------------
	
			var sm = new Ext.grid.CheckboxSelectionModel();
			var resultWidth = Ext.get("result").getWidth();
			var cm = new Ext.grid.ColumnModel([ 
			    new Ext.grid.RowNumberer(), //table's index id
			    sm, 
			        {header:'角色ID',dataIndex:'id',width:resultWidth*0.2,sortable:true,renderer:function(value, p, record){ 
				        return String.format("<a href=\"javascript:submitFunc('{0}','{1}','{2}','{3}')\">{1}</a>", 
						        value,
						        record.data.id,
						        record.data.rolename,
						        record.data.descn					        
				        );
				        }}, 
				    {header:'角色名称',dataIndex:'rolename',width:resultWidth*0.2,sortable:true}, 	
			        {header:'描述',dataIndex:'descn',width:resultWidth*0.4,sortable:true}, 	        
			        {header:'操作',dataIndex:'',width:resultWidth*0.2,sortable:true,renderer:function(value, p, record){
			        	return String.format("<a href=\"javascript:submitFuncResource('{0}')\">资源</a>", record.data.id);
			        }}
			    ]); 
			    cm.defaultSortable = true; 
			    
			    //将结果转换成JSON再传到页面
			 	var ds = new Ext.data.Store({ 
			        proxy: new Ext.data.HttpProxy({
			        	url:'${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/roleModule/roleAction/QueryRoleList.action'
			        }), 
			        reader: new Ext.data.JsonReader({ 
			                totalProperty: 'count', 
			                root: 'result' 
				    }, 
				    [ 
				        {name: 'id',mapping:'id'}, 
				        {name: 'rolename',mapping:'name'}, 
				        {name: 'descn',mapping:'descn'}, 
				    ])			    
			    }); 
			    
			     var grid = new Ext.grid.GridPanel({ 
				    el: 'result', 
				    title:'角色列表', 
				    height:600, 
				    bodyStyle:'width:100%',
				    stripeRows: true,
				    collapsible:true,
				    trackMouseOver:false, 
				    loadMask: {msg:'正在加载数据，请稍侯……'}, 
				    store:ds, 
				    cm: cm, 
				    sm:sm, 
				    tbar: [{
			            text:'添加角色',
			            tooltip:'添加角色',
			            iconCls:'add',
			            handler: function(){
			            	window.location = '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/roleModule/rolePage/roleInsert.jsp'
			            }
			        }, '-',{
			            text:'删除角色',
			            tooltip:'删除角色',
			            iconCls:'remove',
			            handler : remove
			        }, '-',{
			        	text:'返回',
			        	tooltip:'返回',
			        	iconCls:'back',
			        	handler: function() {
			        		window.location = '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/roleModule/rolePage/roleList.jsp';
			        	}
			        }],
				    bbar: new Ext.PagingToolbar({ 
				            pageSize: 20, 
				            store: ds, 
				            displayInfo: true, 
				            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录', 
				            emptyMsg: "没有数据显示" 
				    }) 
				}); 
				
				ds.load({params:{start:0, limit:20}}); 
			    grid.render(); 	
			    
				ds.on('beforeload',function(){
				      Ext.apply(
				      this.baseParams,
				      {
				          	roleId:Ext.get("roleId_filter").dom.value 
				      });
				      if(!(top.form.isValid())){
				      	Ext.MessageBox.alert('警告','对不起，您的查询条件有误！');
			    		return false;
			    	 }
			    	}	
			    );
				
				//--------------------------------------------------------------------------------------------
				//						删除角色
				//
				//--------------------------------------------------------------------------------------------
			  	function remove(){
			  	
			  		arr_Records = sm.getSelections();
				    if (arr_Records.length == 0) {
				        Ext.MessageBox.alert('警告', '请选择要删除的记录！');
				    } else {
						Ext.MessageBox.confirm('确认', '确认删除所选记录?', delRole);
					}
				}
				
				function delRole(btn){
			
			        if(btn == 'yes'){
				        arr_Records = sm.getSelections();
				        var delStr = ''; 		 
				   	   	for (var i = 0, len = arr_Records.length; i < len; i++){    				   	   	  	 
				   	   	  	 delStr += arr_Records[i].data.id;  
				   	   	  	 delStr += ',' ;	
						} 
					    if(delStr.length > 0){
			 					delStr = delStr.substring(0,delStr.length - 1) ;
						}
						Ext.Ajax.request({
						     method : 'POST',                             //发送方式.'POST'或'GET',一般是'POST'
						     //发送到页面
						     url: '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl::/roleModule/roleAction/DeleteRole.action',                
						     success:function(request){                   //发送成功的回调函数
						       var myResult = Ext.util.JSON.decode(request.responseText);
						   	   Ext.Msg.alert('信息','恭喜你，删除成功！',function() {ds.load({params:{start:0, limit:20}});});        //弹出对话框
						     },
						     failure:function(){                          //发送失败的回调函数
						       Ext.Msg.alert("错误", "发送失败，请重试！");
						     },
						     params:{
						     		roleSerialnoObject:delStr 
						     } 
				     	});

			        	 //重新刷新table
			        } 
			    }; 
			    
	//--------------------------------------------------------------------------------------------
	//						下面是修改角色和添加角色
	//
	//--------------------------------------------------------------------------------------------    
  	
	submitFunc = function(id,roleName,descn){
		Ext.get("id").dom.value = id;
		var form = selectRole.getForm().getEl().dom;   
        form.action = '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/roleModule/rolePage/roleEdit.jsp';   
        form.target='_self';   
        form.submit(); 
	};
	
	submitFuncResource = function(id){
		Ext.get("id").dom.value = id;
		var form = selectRole.getForm().getEl().dom;   
        form.action = '${pageContext.request.contextPath}/edu.ccut.saturn.authority.impl/roleModule/rolePage/roleResourceList.jsp';   
        form.target='_self';   
        form.submit(); 
	}; 
	
	var selectRole = new Ext.FormPanel({
    	id:'selectRole',
        labelWidth: 75, 
        frame:true,
        title: 'selectRole',
        bodyStyle:'padding:5px 5px 0',
        defaults: {width: 230},
        defaultType: 'textfield',
        items: [{
                name: 'id',
                id: 'id'
            }]
    });
    selectRole.render('selectRole');
    selectRole.hide();
				
	 var selectResource = new Ext.FormPanel({
    	id:'selectResource',
        labelWidth: 75, 
        frame:true,
        title: 'selectResource',
        bodyStyle:'padding:5px 5px 0',
        defaults: {width: 230},
        defaultType: 'textfield',
        items: [{
                name: 'roleId',
                id: 'roleId'
            }
        ]
    });
    selectResource.render('selectResource');
    selectResource.hide();	
    document.getElementById('roleId_filter').maxLength = 10;						
});
</script>


